﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConfeccionesJR.Entites;
using ConfeccionesJR.BussinesLogic;
using Microsoft.Practices.Unity;

namespace ConfeccionesJR.UI
{
    public partial class FrmProveedor : Form
    {
        [Dependency]
        public IProveedorBL proveedorBL { get; set; }

        public FrmProveedor()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void FrmProveedor_Load(object sender, EventArgs e)
        {
            pROVEEDORBindingSource.DataSource = proveedorBL.GetAllFromProveedor();
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            pROVEEDORBindingSource.DataSource = proveedorBL.GetFromProveedorByDescripcion(TxtDescripcion.Text);
        }

        private void TxtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                pROVEEDORBindingSource.DataSource = proveedorBL.GetFromProveedorByDescripcion(TxtDescripcion.Text);
            }
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            FrmMttoProveedor frm = new FrmMttoProveedor();
            frm.Nuevo();
            frm.ShowDialog();
            pROVEEDORBindingSource.DataSource = proveedorBL.GetAllFromProveedor();
        }

        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            PROVEEDOR currentProveedor = pROVEEDORBindingSource.Current as PROVEEDOR;
            if (currentProveedor != null)
            {
                FrmMttoProveedor frm = new FrmMttoProveedor();
                frm.CurrentProveedor = currentProveedor;
                frm.Editar();
                frm.ShowDialog();
                pROVEEDORBindingSource.DataSource = proveedorBL.GetAllFromProveedor();
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Proveedor", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        public PROVEEDOR CurrentProveedor { get; set; }

        public bool Prove { get; set; }

        private void pROVEEDORDataGridView_DoubleClick(object sender, EventArgs e)
        {
            if (Prove == true)
            {
                CurrentProveedor = pROVEEDORBindingSource.Current as PROVEEDOR;
                Close();
            }
        }

        private void TxtDescripcion_KeyPress(object sender, KeyPressEventArgs e)
        {
            var guion = Convert.ToString("-");
            var punto = Convert.ToString(".");
            var amberson = Convert.ToString("&");

            string cadena = guion + amberson + punto + (char)8;

            if (!cadena.Contains(e.KeyChar) && !(char.IsLetter(e.KeyChar)) && !(char.IsNumber(e.KeyChar))
                && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear)
                && !(char.IsSeparator(e.KeyChar)))
            {
                e.Handled = true;
                return;
            }
        }


    }
}
